import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:tracker/pages/home/home/view/home_page.dart';
import 'package:tracker/pages/profile/view/profile_page.dart';
import 'package:tracker/router/router_manager.dart';
import 'package:tracker/theme/app_theme.dart';
import 'package:tracker/pages/home/location/view/location_page.dart';

import '../../../../common/push/jpush_utils.dart';
import '../../../../startup/config/app_start.dart';
import '../../../../utils/upgrade_utils.dart';
import '../mixin/AppStoreJump.dart';

class BottomNavigatorPage extends ConsumerStatefulWidget {
  const BottomNavigatorPage({super.key});

  @override
  ConsumerState<BottomNavigatorPage> createState() => _BottomNavigatorPageState();
}

class _BottomNavigatorPageState extends ConsumerState<BottomNavigatorPage> {
  int _currentIndex = 0;
  final _pageController = PageController(
    initialPage: 0,
  );
  double _tabbarSize = 24;

  final List<Widget> _pages = [const LocationPage(), const HomePage(), const ProfilePage()];

  /// 根据页面地址打开页面
  _openPageByPath(BuildContext context, String path) {
    if (path.startsWith('http')) {
      context.push('${RouterManager.web}?url=${Uri.encodeComponent(path)}');
    } else {
      context.push(path);
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _pageController.dispose();
  }

  @override
  void initState() {
    super.initState();
    // 在 initState 中执行异步初始化
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      print('Appstart 初始化:');

      await UpgradeUtils.initXUpdate();
      // UpgradeUtils.checkUpdate(context, fromPage: 'home');

      UpdateManager.checkAndShowUpdate(context);

    });
  }


  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: PageView(
        controller: _pageController,
        physics: const NeverScrollableScrollPhysics(),
        children: _pages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          _pageController.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
        selectedItemColor: AppTheme.themeConfig.primaryColor,
        unselectedItemColor: AppTheme.themeConfig.infoTextColor,
        items: [
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/tabbar/location.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              activeIcon: Image.asset(
                'assets/images/tabbar/location_selected.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              label: '定位'),
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/tabbar/controllerCenter.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              activeIcon: Image.asset(
                'assets/images/tabbar/controller_selected.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              label: '控制中心'),
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/tabbar/mine.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              activeIcon: Image.asset(
                'assets/images/tabbar/mine_selected.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              label: '我的'),
        ],
      ),
    );
  }
}
